/*********************************************************************************************************************
 * COPYRIGHT NOTICE
 * Copyright (c) 2018,逐飞科技
 * All rights reserved.
 * 技术讨论QQ群：	一群：179029047(已满)
 *					二群：244861897(已满)
 *					三群：824575535
 *
 * 以下所有内容版权均属逐飞科技所有，未经允许不得用于商业用途，
 * 欢迎各位使用并传播本程序，修改内容时必须保留逐飞科技的版权声明。
 *
 * @file			seekfree_esp8266.h
 * @company			成都逐飞科技有限公司
 * @author			逐飞科技(QQ3184284598)
 * @version			查看doc内version文件 版本说明
 * @Software		IAR 8.3 or MDK 5.24
 * @Taobao			https://seekfree.taobao.com/
 * @date			2020-03-25
 * @note			本文件作为IOT开发板 seekfree_esp8266 2.4G WIFI 模块AT指令 API
 ********************************************************************************************************************/
#ifndef _SEEKFREE_ESP8266_H_
#define _SEEKFREE_ESP8266_H_

#ifdef IOT_BOARD
#include "iot_flexcomm_usart.h"
#else
#include "zf_usart.h"
#include "zf_gpio.h"
#endif

typedef enum
{
	ESP8266_CWMODE_STATION				= 0x01,
	ESP8266_CWMODE_SOFTAP				= 0x02,
	ESP8266_CWMODE_STATION_STATION		= 0x03,
}esp8266_cwmode_list;

typedef struct
{
	char wifi_ssid[21];								// wifi 名称
	uint8_t wifi_ssid_len;							// wifi 名称长度
	char wifi_pwd[21];								// wifi 密码
	uint8_t wifi_pwd_len;							// wifi 密码长度
	uint8_t wifi_bssid[6];							// wifi 选填参数 当目标 AP 多个重名时 指定 MAC 地址 一般用于有多个 SSID 相同的 AP 的情况
}esp8266_ap_message_t;

#define MAX_ESP8266_MESSAGE_LEN			(256+10)		// 这里 +10 是因为 ESP8266 接收数据会有包头

#ifdef IOT_BOARD
void esp8266_init (iot_flexcomm_index_list Index);

uint8_t esp8266_restart (iot_flexcomm_index_list Index);
uint8_t esp8266_set_cwmode (iot_flexcomm_index_list Index, esp8266_cwmode_list mode);

void esp8266_ap_set_wifi_ssid (char *name, uint8_t size);
void esp8266_ap_set_wifi_pwd (char *pwd, uint8_t size);
uint8_t esp8266_ap_connect (iot_flexcomm_index_list Index);

uint8_t esp8266_great_tcp_connect (iot_flexcomm_index_list Index, char *ip, uint8_t ip_len, char *port, uint16_t port_len);
uint8_t esp8266_great_udp_connect (iot_flexcomm_index_list Index, char *ip, uint8_t ip_len, char *port, uint16_t port_len);
uint8_t esp8266_get_local_ip (iot_flexcomm_index_list Index, char ip[16], uint8_t *ip_len);
uint8_t esp8266_send_message (iot_flexcomm_index_list Index, char *data, uint32_t data_len);

uint8_t esp8266_receive_message (iot_flexcomm_index_list Index);
void esp8266_clear_receive_buffer (void);
uint16_t esp8266_read_receive_buffer (char *receive_buffer);
void esp8266_handler (void);
#else
void esp8266_init (flexcomm_index_list Index);

uint8_t esp8266_restart (flexcomm_index_list Index);
uint8_t esp8266_set_cwmode (flexcomm_index_list Index, esp8266_cwmode_list mode);

void esp8266_ap_set_wifi_ssid (char *name, uint8_t size);
void esp8266_ap_set_wifi_pwd (char *pwd, uint8_t size);
uint8_t esp8266_ap_connect (flexcomm_index_list Index);

uint8_t esp8266_great_tcp_connect (flexcomm_index_list Index, char *ip, uint8_t ip_len, char *port, uint16_t port_len);
uint8_t esp8266_great_udp_connect (flexcomm_index_list Index, char *ip, uint8_t ip_len, char *port, uint16_t port_len);
uint8_t esp8266_get_local_ip (flexcomm_index_list Index, char ip[16], uint8_t *ip_len);
uint8_t esp8266_send_message (flexcomm_index_list Index, char *data, uint32_t data_len);

uint8_t esp8266_receive_message (flexcomm_index_list Index);
void esp8266_clear_receive_buffer (void);
uint16_t esp8266_read_receive_buffer (char *receive_buffer);
void esp8266_handler (void);
#endif

#endif
